﻿using System;
using System.Collections;
using UnityEngine;

// Token: 0x02000284 RID: 644
public class CustomAudioSource : MonoBehaviour
{
	// Token: 0x170001A5 RID: 421
	// (get) Token: 0x06000E28 RID: 3624 RVA: 0x0006E477 File Offset: 0x0006C677
	// (set) Token: 0x06000E29 RID: 3625 RVA: 0x0006E47F File Offset: 0x0006C67F
	public AudioSource audioSource { get; private set; }

	// Token: 0x06000E2A RID: 3626 RVA: 0x0006E488 File Offset: 0x0006C688
	private void Awake()
	{
		this._transform = base.transform;
		this.audioSource = base.gameObject.AddComponent<AudioSource>();
		this.audioSource.playOnAwake = false;
		this.audioSource.dopplerLevel = 0f;
		this.audioSource.rolloffMode = AudioRolloffMode.Linear;
		this.audioSource.outputAudioMixerGroup = SoundManager.mg_effects;
		this.FindEntity();
		this.maxDistance = 5f;
		this.audioSource.maxDistance = this.audioSource.maxDistance * this.GetScale();
		this.audioSource.minDistance = 0f;
		this.minDistance = this.audioSource.minDistance;
	}

	// Token: 0x06000E2B RID: 3627 RVA: 0x0006E53C File Offset: 0x0006C73C
	private void Update()
	{
		float scale = this.GetScale();
		this.audioSource.maxDistance = this.maxDistance * scale;
		this.audioSource.minDistance = this.minDistance * scale;
	}

	// Token: 0x06000E2C RID: 3628 RVA: 0x0006E578 File Offset: 0x0006C778
	private float GetScale()
	{
		if (this.entity == null)
		{
			return this._transform.lossyScale.y;
		}
		return this.entity.AccurateScale;
	}

	// Token: 0x06000E2D RID: 3629 RVA: 0x0006E5B5 File Offset: 0x0006C7B5
	public void Play()
	{
		base.StartCoroutine(this.PlayRoutine());
	}

	// Token: 0x06000E2E RID: 3630 RVA: 0x0006E5C4 File Offset: 0x0006C7C4
	private IEnumerator PlayRoutine()
	{
		AudioClip clip = this.audioSource.clip;
		if (clip)
		{
			while (clip.loadState == AudioDataLoadState.Loading)
			{
				yield return null;
			}
			if (clip.loadState == AudioDataLoadState.Loaded)
			{
				this.audioSource.Play();
			}
			else
			{
				Debug.LogError("Load of audioclip " + clip.name + " has failed");
			}
		}
		yield break;
	}

	// Token: 0x06000E2F RID: 3631 RVA: 0x0006E5DF File Offset: 0x0006C7DF
	public void PlayDelayed(float delay)
	{
		base.StartCoroutine(this.PlayDelayedRoutine(delay));
	}

	// Token: 0x06000E30 RID: 3632 RVA: 0x0006E5F0 File Offset: 0x0006C7F0
	private IEnumerator PlayDelayedRoutine(float delay)
	{
		AudioClip clip = this.audioSource.clip;
		while (clip.loadState == AudioDataLoadState.Loading)
		{
			yield return null;
		}
		if (clip.loadState == AudioDataLoadState.Loaded)
		{
			this.audioSource.PlayDelayed(delay);
		}
		else
		{
			Debug.LogError("Load of audioclip " + clip.name + " has failed");
		}
		yield break;
	}

	// Token: 0x06000E31 RID: 3633 RVA: 0x0006E612 File Offset: 0x0006C812
	public void PlayOneShot(AudioClip clip, float volumeScale)
	{
		base.StartCoroutine(this.PlayOneShotRoutine(clip, volumeScale));
	}

	// Token: 0x06000E32 RID: 3634 RVA: 0x0006E624 File Offset: 0x0006C824
	private IEnumerator PlayOneShotRoutine(AudioClip clip, float volumeScale)
	{
		while (clip.loadState == AudioDataLoadState.Loading)
		{
			yield return null;
		}
		if (clip.loadState == AudioDataLoadState.Loaded)
		{
			this.audioSource.PlayOneShot(clip, volumeScale);
		}
		else
		{
			Debug.LogError("Load of audioclip " + clip.name + " has failed");
		}
		yield break;
	}

	// Token: 0x06000E33 RID: 3635 RVA: 0x0006E64D File Offset: 0x0006C84D
	public void PlayOneShot(AudioClip clip)
	{
		base.StartCoroutine(this.PlayOneShotRoutine(clip));
	}

	// Token: 0x06000E34 RID: 3636 RVA: 0x0006E660 File Offset: 0x0006C860
	private IEnumerator PlayOneShotRoutine(AudioClip clip)
	{
		while (clip.loadState == AudioDataLoadState.Loading)
		{
			yield return null;
		}
		if (clip.loadState == AudioDataLoadState.Loaded)
		{
			this.audioSource.PlayOneShot(clip);
		}
		else
		{
			Debug.LogError("Load of audioclip " + clip.name + " has failed");
		}
		yield break;
	}

	// Token: 0x06000E35 RID: 3637 RVA: 0x0006E684 File Offset: 0x0006C884
	private void FindEntity()
	{
		Transform transform = this._transform;
		while (this.entity == null && transform != null)
		{
			this.entity = transform.GetComponent<EntityBase>();
			transform = transform.parent;
		}
	}

	// Token: 0x04001316 RID: 4886
	public float maxDistance;

	// Token: 0x04001317 RID: 4887
	public float minDistance;

	// Token: 0x04001318 RID: 4888
	private Transform _transform;

	// Token: 0x04001319 RID: 4889
	private EntityBase entity;
}
